package com.qqtem.grupo14;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/* This is going to be the main activity */
public class qqTem extends Activity {
	public static final String USER_ID = "USER_ID";

	// TODO:Implement listeners
	private void recSenhaOnClick(View v) {
		// TODO Auto-generated method stub

	}

	private void cadastrarOnClick(View v) {
		// TODO Auto-generated method stub
		Intent i = new Intent(qqTem.this, RegisterUserActivity.class);
		startActivity(i);
	}

	private void entrarAnonOnClick(View v) {
		// TODO Auto-generated method stub
		Intent i = new Intent(qqTem.this, HelloGoogleMaps.class);
		//Intent i = new Intent(qqTem.this, mostrarLocal.class);
		//Intent i = new Intent(qqTem.this, QqTemDraft2Activity.class);
		i.putExtra(USER_ID, -1);
		
		startActivity(i);
	}

	public void entrarOnClick(View v) {
		EditText lg = (EditText) findViewById(R.id.loginField);
		EditText pwd = (EditText) findViewById(R.id.passField);
		
		if (lg.getText().length() > 0) {
			if (DatabaseAdapter.getSingletonInstance().validateLogin(
					lg.getText().toString(), pwd.getText().toString())) {
				Intent i = new Intent(qqTem.this, HelloGoogleMaps.class);
				Cursor c = DatabaseAdapter.getSingletonInstance()
				.fetchUser(lg.getText().toString());
				
				i.putExtra(USER_ID, c.getString(0));
				startActivity(i);
			} else {
				Builder alert = new AlertDialog.Builder(this);
				alert.setMessage(R.string.login_dadoinvalido);
				alert.setNeutralButton(R.string.ok, null);
				alert.show();
			}
		} else {
			Builder alert = new AlertDialog.Builder(this);
			alert.setMessage(R.string.login_datamissing);
			alert.setNeutralButton(R.string.ok, null);
			alert.show();
		}

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Button entrar, entraranon, cadas;
		TextView rec;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		DatabaseAdapter.createSingletonInstance(this);

		entrar = (Button) findViewById(R.id.entrarButton);
		entrar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				entrarOnClick(arg0);
			}
		});

		entraranon = (Button) findViewById(R.id.anonButton);
		entraranon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				entrarAnonOnClick(v);
			}
		});

		cadas = (Button) findViewById(R.id.cadastrarButton);
		cadas.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cadastrarOnClick(v);
			}
		});

		rec = (TextView) findViewById(R.id.login_recuperar);
		rec.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				recSenhaOnClick(v);
			}
		});
	}
}
